package com.example.springboot.demo.thread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class JDBCUtil {

    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
    // 数据库的用户名与密码，需要根据自己的设置
    private static final String USER = "root";
    private static final String PASS = "root";

    private static ExecutorService threadPool = Executors.newFixedThreadPool(50);

    public static void executeBatch(){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            String sql = "insert into test_user1(id, name, phone) values(?,?,?)";
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            for (int j = 0; j < 100; j++) {
                String mobile = "13356544556";
                Integer randNum = 0;
                Random random = new Random();
//                for (int i = 0; i < 1000; i++) {
//                    randNum = random.nextInt(10);
//                }
                randNum = random.nextInt(10);
                String uid = UUID.randomUUID().toString().replaceAll("-","");
                ps.setString(1, uid);
                ps.setString(2, "官德志");
                ps.setString(3, mobile + String.valueOf(randNum));
                ps.addBatch();
            }
            ps.executeBatch();
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        long beginTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(30);
        for (int i = 0; i < 30; i++) {
            System.out.println(i);
            threadPool.execute(() -> {
                try {
                    System.out.println(latch.getCount());
                    JDBCUtil.executeBatch();
                } catch (Exception e) {
                    System.out.println("插入数据异常");
                } finally {
                    latch.countDown();
                    System.out.println(latch.getCount()+"插入数据异常");
                }
            });
        }
        System.out.println(latch.getCount());
        latch.await();
        long endTime = System.currentTimeMillis();
        System.out.println("插入100万数据用时：" + (endTime - beginTime) / 1000 + " 秒");
        threadPool.shutdown();
    }
}
